<template>
    <div class="layout-header">
        <div class="layout-header-content">
            <!-- 左侧logo -->
            <div class="layout-nav-logo flex-start-center">
                <img src="@/assets/images/layouts/logo.png" />
                <div class="title">{{ allConfig.appTitle }}</div>
            </div>
            <!-- 中间菜单 -->
            <div class="layout-nav-menu flex-center-center">
                <FpiMenu
                    @handleSelectedMenu="handleSelectedMenu"
                    :menuList="menuInfo.list"
                    :active-code="menuInfo.activeCode"
                />
            </div>
            <!-- 右侧用户信息 -->
            <div class="layout-nav-user flex-end-center">
                <el-popover placement="bottom" trigger="hover" popper-class="user-popover">
                    <template #reference
                        ><img src="@/assets/images/layouts/user.png" style="cursor: pointer"
                    /></template>
                    <template #default>
                        <div class="li login-out" @click="outLogin">登出</div>
                    </template>
                </el-popover>
            </div>
        </div>
        <div class="layout-header-empty">用于占据头部菜单的空位</div>
    </div>
</template>
<script lang="ts" setup>
import FpiMenu from './HeaderMenu/FpiMenu.vue'
import { outLoginApi } from '@/services/global'
import { redirectLogin } from '@/utils/tools'
import { allConfig } from '@/config'
import { menuList } from '@/router/menuList'
const router = useRouter()
const route = useRoute()

/**
 * 菜单导航相关逻辑
 */
const menuInfo = reactive<any>({
    list: menuList,
    activeCode: '',
})
// 监听路由更新active项
watch(
    () => route.path,
    newPath => {
        if (newPath !== '/') {
            menuInfo.activeCode = newPath
        }
    },
    { immediate: true },
)
// 菜单路由跳转
const handleSelectedMenu = (code: string) => {
    router.push(code)
}

/**
 * 登出
 */
const outLogin = async () => {
    await outLoginApi()
    redirectLogin()
}
</script>
<style lang="scss" scoped>
.layout-header-empty {
    height: 40px;
    color: transparent;
}

.layout-header-content {
    position: fixed;
    top: 0;
    left: 0;
    z-index: 1;
    display: flex;
    align-items: center;
    justify-content: space-between;
    width: 100%;
    height: 40px;
    padding: 0 16px;
    background: #0151e3;

    .title {
        margin-left: 10px;
        font-family: PangMenZhengDao;
        font-size: 24px;
        color: #fff;
    }
}

.li {
    padding: 8px;
    cursor: pointer;

    &:hover {
        color: #409eff;
        background: #e6f7ff;
    }
}

.layout-nav-menu {
    flex: 1;
}

.layout-nav-logo,
.layout-nav-user {
    width: 320px;
}
</style>
<style>
.user-popover {
    padding: 8px 0 !important;
}
</style>
